package rhinestone.Model;

import java.util.HashMap;
import java.io.Serializable;

/**
 * Container which holds all the machinery for lease.
 *
 * @author Group 2
 * @version 2010.12.16
 */
public class MachineryContainer implements Serializable {
	private HashMap<String, Machinery> machines;
	private static MachineryContainer instance;
	private boolean changed;

	/**
	 * Private constructor for instance variable initialization.
	 */
	private MachineryContainer() {
		machines = new HashMap<String, Machinery>();
		changed = false;
	}

	/**
	 * Singleton pattern.
	 * @return The object of this container.
	 */
	public static MachineryContainer getInstance() {
		if(instance == null)
			instance = new MachineryContainer();
		return instance;
	}

	/**
	 * Adds machinery in the database.
	 *
	 * @param machinery The machinery to be added.
	 */
	public void addMachinery(Machinery machinery) {
		machines.put(machinery.getBarcode(), machinery);
		changed = true;
	}

	/**
	 * Looks for a machinery with provided bar code.
	 *
	 * @param barcode The bar code of the machinery.
	 * @return The Machinery object (null if not found).
	 */
	public Machinery getMachinery(String barcode) {
		return machines.get(barcode);
	}

	/**
	 * Removes machinery with provided bar code.
	 * Also it changes the status of container as changed,
	 * so before exiting the program, this container will be serialized.
	 *
	 * @param barcode The bar code of machinery.
	 * @return The deleted machinery object.
	 */
	public Machinery removeMachinery(String barcode){
		changed = true;
		return machines.remove(barcode);
	}

	/**
	 * Getter and setter of container status.
	 */
	public boolean isChanged() {
		return changed;
	}
	public void setChanged(boolean changed) {
		this.changed = changed;
	}
}
